<template>
  <div class="login">
    <el-form ref="form" :model="form" status-icon :rules="rules" label-width="80px">
      <img src="../assets/57431111841191 副本.jpg" alt="" class="img-tou">
      <el-form-item label="用户名:" prop="username">
        <el-input v-model="form.username" @keyup.enter.native = "login"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="form.password" type="password" @keyup.enter.native = "login"></el-input>
      </el-form-item>
      <el-form-item>
       <el-button type="primary" class="login-btn" @click="login">登录</el-button>
       <el-button type="warning" @click="reset">重置</el-button>
       </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 3, max: 12, message: '长度在 3 - 12 字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { min: 3, max: 12, message: '长度在 3 - 12 字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(isflag => {
        if (!isflag) return
        this.$axios.post('login', this.form).then(res => {
          const { meta, data } = res
          console.log(meta)
          if (meta.status === 200) {
            this.$message({
              type: 'success',
              message: '可以下班回家了',
              showClose: true,
              duration: 1500
            })
            localStorage.setItem('token', data.token)
            this.$router.push('/index')
          } else {
            this.$message.error({
              message: meta.msg,
              showClose: true,
              duration: 1500
            })
          }
        })
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less">
.login {
  background-color: #2d434c;
  width: 100%;
  height: 100%;
  overflow: hidden;
  .el-form {
    position: relative;
    background-color: #fff;
    width: 400px;
    padding: 20px;
    padding-top: 80px;
    margin: 0 auto;
    margin-top: 200px;
    border-radius: 20px;
    .login-btn {
      margin-right: 70px
    }
    .img-tou {
      position: absolute;
      top: -40px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 50%;
      border: 3px solid #fff;
      width: 100px;
      height: 100px;
    }
  }
}
</style>
